package org.fas.tween
{
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import org.fas.utils.FuMath;
	
	public class TransTween extends EventDispatcher
	{
		public var listener_frame:Function;
		public var lisHalf:Function;
		public var listener_complete:Function;
		public var isRun:Boolean = false;
		private var mc:Object;
		private var prop:String;
		private var value:Number = 0;
		private var startValue:Number = 0;
		private var stopValue:Number = 0;
		private var timeSum:Number = 0;
		private var timeNow:Number = 0;
		private var tranType:String = "--";//-- == ++ 
		public function TransTween(){
		}
		public function fix(_mc:Object,_prop:String,_startValue:Number,_stopValue:Number,_frameSum:Number,_tranType:String="--"):void{
			mc = _mc;
			prop = _prop;
			timeSum = _frameSum;
			startValue= _startValue;
			stopValue = _stopValue;
			tranType = _tranType;
		}
		private var interId:int;
		public function start():void{
			value = startValue;
			timeNow = 0;
			stop();
			isRun = true;
			interId = flash.utils.setInterval(this.framehandle,10);
//			this.addEventListener(Event.ENTER_FRAME,framehandle);
		}
		private function framehandle(_e:Event=null):void{
			timeNow++;
			var lastValue:Number=value;
			//speed
//			value = (stopValue - startValue)/timeSum*timeNow+startValue;
			//sin    quick - slow
			switch(tranType){
				case "--":
					value = startValue + (stopValue - startValue)*FuMath.sin(90/timeSum*timeNow);
					break;
				case "==":
					value = (stopValue - startValue)/timeSum*timeNow+startValue;
					break;
			}
			if(timeNow == timeSum){
				complete();
			}else{
				if(timeNow==int(timeSum*2/4) && lisHalf!=null){
					lisHalf(value);
				}
				mc[prop] = value;
				if(this.hasEventListener(Event.CHANGE)){
					var _e:Event = new Event(Event.CHANGE);
					_e.currentTarget.value = value;
					this.dispatchEvent(_e);
				}
//				if(listener_frame!=null){
//					listener_frame(value);
//				}
			}
		}
		public function stop():void{
			isRun = false;
			flash.utils.clearInterval(this.interId);
//			this.removeEventListener(Event.ENTER_FRAME,framehandle);
		}
		public function complete():void{
			mc[prop] = stopValue;
			stop();
			mc = null;
			prop = "";
//			trace(prop,value);
			if(this.hasEventListener(Event.COMPLETE)){
				var _e:Event = new Event(Event.COMPLETE);
				_e.currentTarget.value = value;
				this.dispatchEvent(_e);
			}
//			if(listener_complete!=null){
//				listener_complete(value);
//				listener_complete = null;
//				listener_frame = null;
//			}
		}
	}
}